import requests, json
from lxml import etree

"""
拿到页面源代码
提取和解析数据
"""

url = 'https://shanghai.zbj.com/search/f/?kw=saas'
resp = requests.get(url)

# 解析
html = etree.HTML(resp.text)
# 拿到每一个服务商的div
divs = html.xpath("/html/body/div[6]/div/div/div[2]/div[5]/div[1]/div")

f = open('猪八戒.csv', 'w', encoding='utf-8')
for div in divs:
    price = div.xpath("./div/div/a[2]/div[2]/div[1]/span[1]/text()")[0]
    title = div.xpath("./div/div/a[2]/div[2]/div[2]/p/text()")[0]
    com_name = div.xpath("./div/div/a[1]/div[1]/p/text()")[1].split(f'\n')[-1]
    f.write(str([price, title, com_name]))
    f.write('\n')
f.close()
print('爬取完毕!!!')
